Count the number of elements within a rangeΒΆ
Count the number of elements in a list within a specified range.
def count_range_in_list(L, min, max):
ctr = 0
for x in L:
if min <= x <= max:
ctr += 1
return ctr
# test
L1 = [10,20,30,40,40,40,70,80,99]
print(count_range_in_list(L1, 40, 100)) # 6
L2 = ['a', 'b', 'c', 'd', 'e', 'f']
print(count_range_in_list(L2, 'a', 'e')) # 5